package cn.iocoder.yudao.module.wms.service.warehouse;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.wms.controller.admin.warehouse.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.warehouse.WarehouseDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wms.dal.mysql.warehouse.WarehouseMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.*;

/**
 * 仓库 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, WarehouseDO> implements WarehouseService {

    @Resource
    private WarehouseMapper warehouseMapper;

    @Override
    public String createWarehouse(WarehouseSaveReqVO reqVO) {
        // 查询仓库名称是否存在，存在则提示
        boolean exists = this.exists(Wrappers.lambdaQuery(WarehouseDO.class)
                .eq(WarehouseDO::getName, reqVO.getName().trim())
        );
        if (exists) {
            throw exception(WAREHOUSE_NAME_EXISTS);
        }
        // 插入
        WarehouseDO warehouseDO = BeanUtils.toBean(reqVO, WarehouseDO.class);
        String now = LocalDateTimeUtil.formatNormal(LocalDateTime.now());
        warehouseDO.setId(UUID.randomUUID().toString())
                .setCreateAt(now)
                .setUpdateAt(now);
        warehouseMapper.insert(warehouseDO);
        // 返回
        return warehouseDO.getId();
    }

    @Override
    public void updateWarehouse(WarehouseSaveReqVO reqVO) {
        // 校验存在
        validateWarehouseExists(reqVO.getId());
        // 查询仓库名称是否存在，存在则提示
        boolean exists = this.exists(Wrappers.lambdaQuery(WarehouseDO.class)
                .ne(WarehouseDO::getId, reqVO.getId())
                .eq(WarehouseDO::getName, reqVO.getName().trim())
        );
        if (exists) {
            throw exception(WAREHOUSE_NAME_EXISTS);
        }
        // 更新
        WarehouseDO updateObj = BeanUtils.toBean(reqVO, WarehouseDO.class);
        updateObj.setUpdateAt(LocalDateTimeUtil.formatNormal(LocalDateTime.now()));
        warehouseMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarehouse(String id) {
        // 校验存在
        validateWarehouseExists(id);
        // 删除
        warehouseMapper.deleteById(id);
    }

    private void validateWarehouseExists(String id) {
        if (warehouseMapper.selectById(id) == null) {
            throw exception(WAREHOUSE_NOT_EXISTS);
        }
    }

    @Override
    public WarehouseDO getWarehouse(String id) {
        return warehouseMapper.selectById(id);
    }

    @Override
    public PageResult<WarehouseDO> getWarehousePage(WarehousePageReqVO pageReqVO) {
        return warehouseMapper.selectPage(pageReqVO);
    }

}